weatherType = {
	"多云": "duoyun",
	"晴": "qing",
	"阴": "yin",
	"小雨": "xiaoyu",
	"雨": "yu",
	"大雨": "dayu",
	"阵雨": "yu"
}

$(function() {
	$.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function(script, textStatus, jqXHR) {
		console.log(remote_ip_info)
		console.log(remote_ip_info['start'])
		city = remote_ip_info['city']; // 获取城市
		// for (var i = 0; i <= 4; i++) {
		// 	$.getScript("http://php.weather.sina.com.cn/js.php?city=" + city + "&day=" + i + "&password=DJOYnieT8234jlsK", function() {
		// 		var str = "<div class='item'><div class='date'>" + savedate_weather + "</div><div class='info'><span><i class='icon-" + figure1 + "'></i>" + temperature1 + "°～" + temperature2 + "°</span></div><div class='city'>" + city + "</div></div>"
		// 		$(".weather").append(str)
		// 	});
		// }
		$.getJSON("http://wthrcdn.etouch.cn/weather_mini?city=深圳", function(json) {
			for (var i = 0; i < json.data.forecast.length; i++) {
				var forecast = json.data.forecast[i];
				high = forecast.high.substr(3);
				low = forecast.low.substr(3);
				date = forecast.date;
				type = weatherType[forecast.type];
				var str = "<div class='item'><div class='date'>" + date + "</div><div class='info'><span><i class='icon-" + type + "'></i>" + low + "～" + high + "</span></div><div class='city'>" + city + "</div></div>"
				$(".weather").append(str)
			};
			console.log(json)
		})
		/*
返回的数据
city='北京';
year1='14';//
month1='02';
day1='21';
year2='14';
month2='02';
day2='22';
city='北京';
savedate_weather='2014-02-21';
savedate_life='2014-02-21';
savedate_zhishu='2014-02-21';
status1='霾';
status2='雾';
figure1='mai';
figure2='wu';
direction1='无持续风向';
direction2='无持续风向';
power1='≤3';
power2='≤3';
temperature1='3';//白天温度
temperature2='-2';//晚上温度

*/

	});
})

require(["/js/fullcalendar/lib/moment.min.js", "/js/fullcalendar/fullcalendar.min.js"], function() {
	// 获取日历事件
	var updata = {};
	var nowevent;
	var obj = eval('(' + $.cookie("loginer") + ')');
	$.ajax({
		url: "/api/complex/MyCalendar",
		type: "get",
		data: {
			"uid": "1"
		},
		success: function(result) {
			console.log(result.result, result);
			$('#calendar').fullCalendar({
				theme: "true",
				header: {
					// left: 'prev,next today',
					left: 'title',
					right: ''
					// right: 'month,agendaWeek,agendaDay'
				},
				events: result.result,

				// 添加点击事件,改变计划状态
				eventClick: function(event, element) {
					console.log(event);
					updata = {}
					nowevent = event;
					$(".check").addClass("focus");
					$("[name='plan']").html(event.title);
					if (event.color == "0") {
						$("[name='alert']").html("点击确定后将把此计划标记为已完成");
						updata.color = "#468847";
					} else {
						$("[name='alert']").html("点击确定后将把此计划还原为未完成");
						updata.color = "0";
					};
					updata.id = event._id;
				}
			});
			$('.fc-title').hover(function() {
				$(this).attr('title', $(this).text())
			});
			$("[name='plantotal']").html(result.length);
			$("[name='finishtotal']").html(result.done);
			$("[name='totalfinish']").html(((result.done / result.length) * 100).toFixed(2) + "%");
		},
		error: function(e) {
			console.log(e);
		}
	});

	// 修改
	$("[name='upcalendarbtn']").click(function() {
		$.ajax({
			url: "/api/complex/MyCalendar",
			type: "put",
			cache: false,
			data: updata,
			// sycn: true,
			success: function(result) {
				console.log(result);
				$(".check").removeClass("focus");
				nowevent.color = updata.color;
				$('#calendar').fullCalendar('updateEvent', nowevent);
			},
			error: function(e) {
				console.log(e);
			}
		});
	});

	// 取消
	$("[name='upcalendarcancel']").click(function() {
		$(".check").removeClass("focus");
	});

	// 删除
	$("[name='upcalendardelete']").click(function() {
		$.ajax({
			url: "/api/complex/MyCalendar",
			type: "delete",
			data: {
				"id": nowevent._id
			},
			success: function(result) {
				$(".check").removeClass("focus");
				$("#calendar").fullCalendar('removeEvents', [nowevent._id]);
				// console.log(result);
			},
			error: function(e) {
				console.log(e);
			}
		});
	});
	// console.log(event);
	// 创建日历事件
	$("[name='createevent']").click(function() {
		var start = $("[name='start']").val();
		var end = $("[name='end']").val();
		if ((end && start > end) || !start) {
			alert("日期选择有误！！！");
			return;
		} else {
			var data = {
				start: start,
				end: end + " 24:00:00" || start
			};
			var title = $("[name='title']").val();
			if (!title) {
				alert("请标注好事件！！！");
				return
			} else {
				data.title = title;
				data.color = 0;
				data.allDay = "true";
				var url = $("[name='url']").val();
				if (url) {
					data.url = url;
				};
				data.id = new Date().getTime();
				data.tid = obj.tid;
				// console.log(data);
				$.ajax({
					url: "/api/complex/MyCalendar",
					type: "post",
					// dataType: "json",
					data: data,
					success: function(result) {
						// console.log(result);
						$("input").val("");
						alert("事件添加成功！！！");
						var ar = [];
						ar[0] = data;
						$('#calendar').fullCalendar('addEventSource', ar);
					},
					error: function(e) {
						console.log(e);
					}
				})
			};
		};
	});

	// 月份
	$("[name='prevmonth']").click(function() {
		$('#calendar').fullCalendar('prev');
	});
	$("[name='nextmonth']").click(function() {
		$('#calendar').fullCalendar('next');
	});
});

// datepicker
require(["/js/datepicker/bootstrap.min.js", "/js/datepicker/bootstrap-datetimepicker.min.js"], function(datepicker) {
	$('#datetimepicker').datetimepicker({
		format: 'yyyy-MM-dd',
		language: 'en',
		pickDate: true,
		// pickTime: true,
		// hourStep: 1,
		// minuteStep: 15,
		// secondStep: 30,
		inputMask: true
	});
	$('#end').datetimepicker({
		format: 'yyyy-MM-dd',
		language: 'en',
		pickDate: true,
		// pickTime: true,
		// hourStep: 1,
		// minuteStep: 15,
		// secondStep: 30,
		inputMask: true
	});
});